package com.qzyh.esb.egsa.exception;

import com.qzyh.esb.egsa.utils.ResultObject;
import io.jsonwebtoken.JwtException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


@ControllerAdvice
public class GlobalDefaultExceptionHandler {
    //通过LogFactory获取Logger类的实例
    private static final Logger logger = LoggerFactory.getLogger(GlobalDefaultExceptionHandler.class);

    @ResponseBody
    @ExceptionHandler(value= JwtException.class)
    public ResultObject tokenInvalidHandler(Exception e) {
        logger.error("token失效-》》GlobalDefaultExceptionHandler.tokenInvalidHandler()");
        return ResultObject.invalid();
    }

    @ResponseBody
    @ExceptionHandler(value=Exception.class)
    public ResultObject defaultErrorHandler(Exception e) {
        logger.error("全局-》》GlobalDefaultExceptionHandler.defaultErrorHandler()");
        logger.error(e.getMessage(),e);
        return ResultObject.error();
    }
}
